import 'package:flutter/material.dart';
import 'package:jiajia_diy/diyProject/diy.dart';
import 'package:jiajia_diy/pages/diy_add_page.dart';
import 'package:jiajia_diy/ui/diy_details_content.dart';
import 'package:flutter/cupertino.dart';
import 'package:jiajia_diy/ui/icon_font.dart';
import 'dart:async';
import 'dart:io';

/*
点击diy列表的卡片后进入该页，展示活动详细信息，并提供修改
 */
class DiyDetails extends StatefulWidget {
  DiyDetails({this.db, this.diyItem, this.diyItems});
  var db;
  Diy diyItem;
  List<Diy> diyItems;
  @override
  State<StatefulWidget> createState() => new DiyDetailsState();
}

class DiyDetailsState extends State<DiyDetails> {
  _readToDiyList() async {
    List diyList = await widget.db.getDiys();
    print('开始初始化数据');
    widget.diyItems.clear();
    setState(() {
      diyList.forEach((diy) => widget.diyItems.add(Diy.fromMap(diy)));
    });
  }

  //进入编辑页面
  Future _openEditDiyDialog() async {
    final result = await Navigator.of(context).push(new MaterialPageRoute(
        fullscreenDialog: true,
        builder: (context) {
          return new DiyAdd(
              db: widget.db,
              diyItem: widget.diyItem,
              diyItems: widget.diyItems);
        }));
    if (result != null) {
      setState(() {
        widget.diyItem = result;
        _readToDiyList();
      });
    } else
      return null;
  }

  @override
  Widget build(BuildContext context) {
    // return new Scaffold(
    //   body: new CustomScrollView(
    //     slivers: <Widget>[
    //       new SliverAppBar(
    //         expandedHeight: 156.0,
    //         actions: <Widget>[
    //           new IconButton(
    //             onPressed: () {
    //               _openEditDiyDialog();
    //             },
    //             icon: new Icon(
    //               DiyIcon1.editItem,
    //               size: 23.0,
    //             ),
    //           ),
    //         ],
    //         pinned: true,
    //         flexibleSpace: new FlexibleSpaceBar(
    //           background: new Image.file(
    //             File(widget.diyItem.path),
    //             fit: BoxFit.cover,
    //           ),
    //           title: new Text(widget.diyItem.name),
    //           centerTitle: true,
    //         ),
    //       ),

    //       // new SliverList(
    //       //   delegate: new SliverChildListDelegate([
    //       //     new Container(
    //       //       child: new Text('aaaaaaa'),
    //       //     ),
    //       //   ]),
    //       // )
    //     ],
    //   ),
    // );
    return new Scaffold(
      body: new NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool inBoxScoll) {
          return <Widget>[
            new SliverAppBar(
              actions: <Widget>[
                new IconButton(
                  onPressed: () {
                    _openEditDiyDialog();
                  },
                  icon: new Icon(
                    DiyIcon1.editItem,
                    size: 23.0,
                  ),
                ),
              ],
              expandedHeight: 156.0,
              floating: false,
              pinned: true,
              flexibleSpace: new FlexibleSpaceBar(
                background: new Image.file(
                  File(widget.diyItem.path),
                  fit: BoxFit.cover,
                ),
                title: new Text(widget.diyItem.name),
                centerTitle: true,
              ),
            )
          ];
        },
        body: new DiyDetailsContent(widget.diyItem),
      ),
    );
  }
}
